<template>
  <div
    :class="['tinymce-container', { fullscreen: fullscreen }]"
    :style="{ width: containerWidth }"
  >
    <textarea :id="id" ref="textarea" v-model="content" />
    <div class="editor-custom-btn-container">
      <editor-image color="#1890ff" class="editor-upload-btn" @successCBK="imageSuccessCBK" />
    </div>
  </div>
</template>

<script>
import editorImage from './components/EditorImage'
import plugins from './plugins'
import toolbar from './toolbar'

// Import TinyMCE
import tinymce from 'tinymce/tinymce'
import 'tinymce/themes/modern/theme'
import 'tinymce/skins/lightgray/skin.min.css'

// Any plugins you want to use has to be imported
import 'tinymce/plugins/advlist'
import 'tinymce/plugins/anchor'
import 'tinymce/plugins/autolink'
import 'tinymce/plugins/autosave'
import 'tinymce/plugins/code'
import 'tinymce/plugins/colorpicker'
import 'tinymce/plugins/colorpicker'
import 'tinymce/plugins/contextmenu'
import 'tinymce/plugins/directionality'
import 'tinymce/plugins/fullscreen'
import 'tinymce/plugins/hr'
import 'tinymce/plugins/image'
import 'tinymce/plugins/imagetools'
import 'tinymce/plugins/insertdatetime'
import 'tinymce/plugins/link'
import 'tinymce/plugins/lists'
import 'tinymce/plugins/media'
import 'tinymce/plugins/nonbreaking'
import 'tinymce/plugins/noneditable'
import 'tinymce/plugins/pagebreak'
import 'tinymce/plugins/paste'
import 'tinymce/plugins/preview'
import 'tinymce/plugins/print'
import 'tinymce/plugins/save'
import 'tinymce/plugins/searchreplace'
import 'tinymce/plugins/tabfocus'
import 'tinymce/plugins/table'
import 'tinymce/plugins/template'
import 'tinymce/plugins/textcolor'
import 'tinymce/plugins/textpattern'
import 'tinymce/plugins/visualblocks'
import 'tinymce/plugins/visualchars'
import 'tinymce/plugins/wordcount'

export default {
  name: 'Tinymce',
  components: { editorImage },
  props: {
    id: {
      type: String,
      default: function() {
        return 'vue-tinymce-' + new Date().getTime()
      }
    },
    value: {
      type: String,
      default: ''
    },
    height: {
      type: [Number, String],
      required: false,
      default: 360
    },
    width: {
      type: [Number, String],
      required: false,
      default: 'auto'
    }
  },
  data() {
    return {
      fullscreen: false,
      content: ''
    }
  },
  computed: {
    containerWidth() {
      const width = this.width
      if (/^[\d]+(\.[\d]+)?$/.test(width)) { // matches `100`, `'100'`
        return `${width}px`
      }
      return width
    }
  },
  mounted() {
    this.init()
  },
  deactivated() {
    this.destroyTinymce()
  },
  destroyed() {
    this.destroyTinymce()
  },
  methods: {
    /**
     * 手动设置内容
     */
    setContent(val) {
      tinymce.get(this.id).setContent(val)
      this.content = val
    },
    init() {
      const options = {
        selector: '#' + this.id,
        skin: false,
        language: 'zh_CN',
        body_class: 'panel-body',
        plugins: plugins,
        toolbar: toolbar,
        height: this.height,
        language_url: 'tinymce/langs/zh_CN.js', // 放到public目录下
        skin_url: `tinymce/skins/lightgray`,
        object_resizing: false,
        convert_urls: false,
        fontsize_formats: '12px 14px 18px 24px 36px 50px',
        imagetools_cors_hosts: ['www.tinymce.com', 'codepen.io'],
        default_link_target: '_blank',
        link_title: false,
        branding: false,
        nonbreaking_force_tab: true,
        paste_retain_style_properties: 'all',
        paste_word_valid_elements: '*[*]',
        paste_convert_word_fake_lists: false,
        style_formats: [{ // 自定义方案
          title: '首行缩进',
          block: 'p',
          styles: { 'text-indent': '2em' }
        }, {
          title: '行高',
          items: [
            { title: '1', styles: { 'line-height': '1' }, inline: 'span' },
            { title: '1.2', styles: { 'line-height': '1.2' }, inline: 'span' },
            { title: '1.5', styles: { 'line-height': '1.5' }, inline: 'span' },
            { title: '2', styles: { 'line-height': '2' }, inline: 'span' },
            { title: '2.5', styles: { 'line-height': '2.5' }, inline: 'span' },
            { title: '3', styles: { 'line-height': '3' }, inline: 'span' }
          ]
        }],
        content_style: `
         * { padding:0; margin:0; }
          html, body { height:100%; }
          img { display:block; height:auto; }
          ul,ol { list-style-position:inside; }
        `,
        setup(editor) {
          editor.on('FullscreenStateChanged', (e) => {
            this.fullscreen = e.state
          })
        },
        init_instance_callback: editor => {
          editor.on('NodeChange Change KeyUp SetContent', () => {
            this.$emit('input', editor.getContent())
          })
        }
      }
      const { origin, pathname } = window.location
      tinymce.baseURL = origin + pathname // 打包到线上需要访问二级目录所以需要baseUrl + language_url 访问依此类推
      tinymce.init(options)
    },
    destroyTinymce() {
      const Tinymce = tinymce.get(this.id)
      if (this.fullscreen) {
        Tinymce.execCommand('mceFullScreen')
      }
      if (Tinymce) {
        Tinymce.destroy()
      }
    },
    imageSuccessCBK(arr) {
      arr.forEach(v => {
        tinymce.get(this.id).insertContent(`<img class="wscnph" src="${v.url}" >`)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tinymce-container {
  position: relative;
  line-height: normal;
}

.tinymce-container {
  ::v-deep {
    .mce-fullscreen {
      z-index: 10000;
    }
  }
}

.tinymce-textarea {
  visibility: hidden;
  z-index: -1;
}

.editor-custom-btn-container {
  position: absolute;
  right: 4px;
  top: 4px;
  /*z-index: 2005;*/
}

.fullscreen .editor-custom-btn-container {
  z-index: 10000;
  position: fixed;
}

.editor-upload-btn {
  display: inline-block;
}
</style>
